﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class InternalNewsConfig:IWebPartConfig
    {
        public string Id
        {
            get
            {
                return "InternalNewsConfig";
            }
        }
        public string Subject 
        { 
            get
            {
                return ResourceBLL.GetText(Id, "Title", "Cấu hình webpart");
            }
        }

        public string RenderWebPart(ConfigParamCls ConfigParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(InternalNewsConfig));
            string Html = "";
           
            string WebPartId = (string)Environments.Request("WebPartId");
            string RecordId = (string)Environments.Request("RecordId");

            string OldGroupNewsId = "";
            string OldGroupNewsItemId = "";
            string XmlConfigItem = Environments.GetXmlWebPartConfigItem(RecordId);
            string SiteCode = Portal.Environment.Environments.GetActiveSite();
            if (XmlConfigItem != null)
            {
                try
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml(new System.IO.StringReader(XmlConfigItem));
                    OldGroupNewsId = (string)ds.Tables[0].Rows[0]["GroupNewsId"];
                    OldGroupNewsItemId = (string)ds.Tables[0].Rows[0]["GroupNewsItemId"];
                    SiteCode = (string)ds.Tables[0].Rows[0]["SiteCode"];
                }
                catch { }
            }
            string SelectGroupNews = "<select id=drpSelectGroupNews onclick=javascript:ClientDetectGroupNewsItem();>";
            DataTable dtGroupNews = new GroupNewsBLL().ReadingGroupNews();
            string FirstGroupNewsItemId = "";
            for (int iIndex = 0; iIndex < dtGroupNews.Rows.Count; iIndex++)
            {
                string GroupNewsId = (string)dtGroupNews.Rows[iIndex]["GroupNewsId"];
                string ItemSubject = (string)dtGroupNews.Rows[iIndex][Environments.ActiveLanguage+ "Subject"];
                if (OldGroupNewsId.Equals(GroupNewsId))
                {
                    FirstGroupNewsItemId = GroupNewsId;
                    SelectGroupNews += "    <option selected value=\"" + GroupNewsId + "\">" + ItemSubject + "</option>";
                }
                else
                {
                    if (FirstGroupNewsItemId.Trim().Length == 0)
                    {
                        FirstGroupNewsItemId = GroupNewsId;
                    }
                    SelectGroupNews += "    <option value=\"" + GroupNewsId + "\">" + ItemSubject + "</option>";
                }
            }
            SelectGroupNews += "</select>";

            //DataTable dtSite = new Portal.SystemWebParts.Site.SiteBLL().Reading();
            //string SelectSiteText = "<select id=\"drpSelectSite\">";
            //for (int iIndex = 0; iIndex < dtSite.Rows.Count; iIndex++)
            //{
            //    string ItemSiteCode=(string)dtSite.Rows[iIndex]["SiteCode"];
            //    if (ItemSiteCode.Equals(SiteCode))
            //    {
            //        SelectSiteText += "<option selected value=\"" + ItemSiteCode + "\">" + ItemSiteCode + "</select>";
            //    }
            //    else
            //    {
            //        SelectSiteText += "<option value=\"" + ItemSiteCode + "\">" + ItemSiteCode + "</select>";
            //    }
            //}
            //SelectSiteText += "</select>";

            Html += 
                  "<script language=javascript>\r\n"+
                  "     function ClientDetectGroupNewsItem()\r\n"+
                  "     {\r\n"+
                  "         GroupNewsIndex=document.getElementById(\"drpSelectGroupNews\").selectedIndex;\r\n" +
                  "         GroupNewsId=document.getElementById(\"drpSelectGroupNews\").options[GroupNewsIndex].value;\r\n" +
                  "         retValue=Portal.WebParts.VBPQ.InternalNewsConfig.DetectGroupNewsItem(GroupNewsId,\"\").value;\r\n" +
                  "         document.getElementById(\"divGroupNewsItem\").innerHTML=retValue;\r\n" +
                  "     }\r\n"+
                  "     function ClientSaveConfig(WebPartId,RecordId)\r\n"+
                  "     {\r\n"+
                  "         GroupNewsIndex=document.getElementById(\"drpSelectGroupNews\").selectedIndex;\r\n" +
                  "         GroupNewsId=document.getElementById(\"drpSelectGroupNews\").options[GroupNewsIndex].value;\r\n" +
                  "         GroupNewsItemIndex=document.getElementById(\"drpSelectGroupNewsItem\").selectedIndex;\r\n" +
                  "         GroupNewsItemId=document.getElementById(\"drpSelectGroupNewsItem\").options[GroupNewsItemIndex].value;\r\n" +
                  //"         SiteIndex=document.getElementById(\"drpSelectSite\").selectedIndex;\r\n"+
                  //"         SiteCode=document.getElementById(\"drpSelectSite\").options[SiteIndex].value;\r\n" +
                  "         rValue=Portal.WebParts.VBPQ.InternalNewsConfig.ServerSideSaveConfig(WebPartId,RecordId,GroupNewsId,GroupNewsItemId).value;\r\n" +
                  "         alert(rValue);\r\n"+
                  "     }\r\n"+
                  "</script>\r\n"+
                  "<table>" +
                  //" <tr>" +
                  //"     <td>" + ResourceBLL.GetText(Id, "SelectGroupNews", "Chỉ hiện ở site") + "</td>" +
                  //"     <td>" + SelectSiteText + "</td>" +
                  //" </tr>" +
                  " <tr>" +
                  "     <td>" + ResourceBLL.GetText(Id, "SelectGroupNews", "Chọn nhóm") + "</td>" +
                  "     <td>" + SelectGroupNews + "</td>" +
                  " </tr>" +
                  " <tr>" +
                  "     <td>" + ResourceBLL.GetText(Id, "SelectNews", "Chọn nhóm tin") + "</td>" +
                  "     <td><div id=divGroupNewsItem>" + DetectGroupNewsItem(FirstGroupNewsItemId, OldGroupNewsItemId) + "</div></td>" +
                  " </tr>" +
                  
                  " <tr>" +
                  "     <td></td>" +
                  "     <td><input onclick=javascript:ClientSaveConfig('"+WebPartId+"','"+RecordId+"'); type=button value=\"" + ResourceBLL.GetText(Id, "Save", "Ghi lại") + "\"><input type=button value=\"" + ResourceBLL.GetText(Id, "Cancel", "Quay lại") + "\" onclick=javascript:window.close();></td>" +
                  " </tr>" +
                  "</table>";
            return Environments.GetFormDialog(Html,Subject);
        }

        public object Reserved { get { return null; } }
        public object SetReserved(object oInput)
        {
            return null;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideSaveConfig(
            string WebPartId,
            string RecordId,
            string GroupNewsId,
            string GroupNewsItemId)
        {
            try
            {
                string XmlConfig = "";
                DataSet ds = new DataSet("XmlConfig");
                ds.Tables.Add("Config");
                ds.Tables["Config"].Columns.Add("WebPartId");
                ds.Tables["Config"].Columns.Add("RecordId");
                ds.Tables["Config"].Columns.Add("GroupNewsId");
                ds.Tables["Config"].Columns.Add("GroupNewsItemId");
                //ds.Tables["Config"].Columns.Add("SiteCode");
                ds.Tables["Config"].Rows.Add(new object[]{
                    WebPartId,RecordId,GroupNewsId,GroupNewsItemId
                });
                XmlConfig = ds.GetXml();
                Environments.SaveWebPartItemConfig(WebPartId, RecordId, XmlConfig);
                return ResourceBLL.GetText(WebPartId, "SaveOk", "Ghi lại cấu hình thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string DetectGroupNewsItem(string GroupNewsId, string OldGroupNewsItemId)
        {
            try
            {
                string SelectGroupNewsItemText = "<select id=drpSelectGroupNewsItem>";
                
                DataTable dtGroupNewsItems = new GroupNewsBLL().ReadingGroupNewsItems(GroupNewsId, null);
                for (int iIndex = 0; iIndex < dtGroupNewsItems.Rows.Count; iIndex++)
                {
                    string GroupNewsItemId = (string)dtGroupNewsItems.Rows[iIndex]["GroupNewsItemId"];
                    string GroupNewsItemName = (string)dtGroupNewsItems.Rows[iIndex][Environments.ActiveLanguage + "GroupNewsItemName"];
                    if (OldGroupNewsItemId.Equals(GroupNewsItemId))
                    {
                        SelectGroupNewsItemText += "  <option selected value=\"" + GroupNewsItemId + "\">" + GroupNewsItemName + "</option>";
                    }
                    else
                    {
                        SelectGroupNewsItemText += "  <option value=\"" + GroupNewsItemId + "\">" + GroupNewsItemName + "</option>";
                    }
                    BuildGroupNewsItem(GroupNewsId, GroupNewsItemId, 4, OldGroupNewsItemId,ref SelectGroupNewsItemText);
                }
                SelectGroupNewsItemText += "</select>";
                return SelectGroupNewsItemText;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        public void BuildGroupNewsItem(string GroupNewsId,string ParentGroupNewsId,int IndexLevel,string OldGroupNewsItemId, ref string SelectGroupNewsItemText)
        {
            DataTable dtGroupNewsItems = new GroupNewsBLL().ReadingGroupNewsItems(GroupNewsId, ParentGroupNewsId);
            for (int iIndex = 0; iIndex < dtGroupNewsItems.Rows.Count; iIndex++)
            {
                string GroupNewsItemId = (string)dtGroupNewsItems.Rows[iIndex]["GroupNewsItemId"];
                string GroupNewsItemName = (string)dtGroupNewsItems.Rows[iIndex][Environments.ActiveLanguage + "GroupNewsItemName"];
                for (int iIndexLevel = 1; iIndexLevel <= IndexLevel; iIndexLevel++)
                {
                    GroupNewsItemName = "=" + GroupNewsItemName;
                }
                if (OldGroupNewsItemId.Equals(GroupNewsItemId))
                {
                    SelectGroupNewsItemText += "  <option selected value=\"" + GroupNewsItemId + "\">" + GroupNewsItemName + "</option>";
                }
                else
                {
                    SelectGroupNewsItemText += "  <option value=\"" + GroupNewsItemId + "\">" + GroupNewsItemName + "</option>";
                }
                IndexLevel = IndexLevel + 4;
                BuildGroupNewsItem(GroupNewsId,GroupNewsItemId,IndexLevel,OldGroupNewsItemId,ref SelectGroupNewsItemText);
                IndexLevel = IndexLevel - 4;
            }
        }

        public void AjaxProReg(System.Web.UI.Page page)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(InternalNewsConfig), page);
        }
    }
}
